package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Division;
import com.sl.au.entity.Grading;
import com.sl.au.entity.Hierarchy;
import com.sl.au.entity.Option;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.service.DivisionService;
import com.sl.au.service.GradingService;
import com.sl.au.service.OptionService;
import com.sl.au.service.UserService;
import com.sl.utils.ExdeUtil;
import com.sl.utils.MsgHelper;

/**
 * 评分等级人员管理
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/gradinguser")
public class GradingUserController extends BaseController {

	@Autowired
	GradingService gradingService;
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	DivisionService divisionService; 
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView modelAndView = this.createLayoutView("grading/gradingUser");
		return modelAndView;
	}
	
	/**
	 * 获取部件区域树信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<Division> node = null;
		
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=divisionService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	/**
	 * 获取层级信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getGrading")
	public Map<String,Object> getGrading(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Grading> list = gradingService.getAllNodes();
		Object[] obj = null;
		if(list != null && list.size() > 0) {
			obj = new Object[list.size()];
			Map<String,String> gradingMap = null;
			Grading grading = null;
			for(int i=0;i<list.size();i++ ) {
				gradingMap = new HashMap<String, String>();
				grading = list.get(i);
				gradingMap.put("name", grading.getGradingName());
				gradingMap.put("value", grading.getGradingLevel().getExtended1());
				gradingMap.put("id", grading.getId());
				obj[i] = gradingMap;
				gradingMap = null;
			}
			
		}
		map.put("data", obj);
		map.put("max", list.size());
		return map;
	}
	
	@RequestMapping(value = "getUserByNotGrading", method = RequestMethod.GET)
	public Map<String, Object> getUserByNoGrading(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<User> users =null;
		int count=0;
		users = userService.getUserByNoGrading(start, length, key, "createTime", Sort.Direction.DESC);
		count = userService.getCountByNoGrading(key);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getUserByGrading", method = RequestMethod.GET)
	public Map<String, Object> getUserByGrading(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String gradingId=request.getParameter("gradingId");
		List<User> users =null;
		int count=0;
		users = userService.getUserByGrading(start, length, gradingId, key, "createTime", Sort.Direction.DESC);
		count = userService.getCountByGrading(gradingId, key);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	
	/**
	 * 绑定层级关系
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addUser",method = RequestMethod.POST)
	public Map<String,Object> addUser(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		String id = (String) map.get("id");
		String gradingId = String.valueOf(map.get("gradingId"));
		User user = userService.getRepository().findOne(id);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		user.setGrading(grading);
		userService.getRepository().save(user);
		msg.put("code", 1);
		msg.put("msg", "人员评分层级绑定成功!");
		return msg;
	};
	
	/**
	 * 移除人员信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/removeUser")
	public Map removeUser(@RequestBody Map<String,Object> map)
	{
		Map<String,Object> msg = new HashMap<String,Object>();
		String id = (String) map.get("id");
		User user = userService.getRepository().findOne(id);
		user.setGrading(null);
		userService.getRepository().save(user);
		msg.put("code", 1);
		msg.put("msg", "人员评分层级移除成功!");
		return msg;
	}
	
	@RequestMapping(value="/addDataAll",method = RequestMethod.POST)
	public Map<String,Object> addDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		String Id = (String) map.get("Id");
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		Grading grading = gradingService.getRepository().findOne(Id);
		for(String userId : dataInfo) {
			User user = userService.getRepository().findOne(userId);
			user.setGrading(grading);
			userService.getRepository().save(user);
		}
		msg.put("msg", "人员评分层级绑定成功!");
		return msg;
	};
	
	@RequestMapping(value="/removeDataAll",method = RequestMethod.POST)
	public Map<String,Object> removeDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		for(String userId : dataInfo) {
			User user = userService.getRepository().findOne(userId);
			user.setGrading(null);
			userService.getRepository().save(user);
		}
		msg.put("msg", "人员评分层级移除成功!");
		return msg;
	};
}
